﻿using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace AutoAccepter
{
    internal class Order
    {
        public string title;
        public string type;
        public string id;
        public string time;
        public string state;
        public string url;

        public Order()
        {
            title = string.Empty;
            type = string.Empty;
            id = string.Empty;
            time = string.Empty;
            state = string.Empty;
            url = string.Empty;
        }

        static public List<Order> Parse(string html)
        {
            List<Order> orderList = new List<Order>();
            var matches = Regex.Matches(html, "<tr class=\"rowBg\\d+?\"[\\s\\S.]+?待办内容[\\s\\S.]+?<a class=\"tablelink\" href=\".+?\">[\\s\\S.]+?(?<title>.+)；[\\s\\S.]+?</a>");
            for (int i = 0; i < matches.Count; i++)
            {
                //var match = Regex.Match(matches[i].Value, "<tr .+?>\\s+<td>(?<type>.*工单.*)</td>\\s+<td>(?<id>.+?)</td>[\\s\\S.]+?主办</td>\\s+<td>(?<time>.+?)</td>[\\s\\S.]+?<td>(?<state>[是否]+)</td>[\\s\\S.]+?<a.+href=\"(?<url>.+?)\".+>\\s+(?<title>.+)；");
                var match = Regex.Match(matches[i].Value, "<tr .+?>\\s+<td>(?<type>.*(?:工单|事件告警).*)</td>\\s+<td>(?<id>.+?)</td>[\\s\\S.]+?主办</td>\\s+<td>(?<time>.+?)</td>[\\s\\S.]+?<td>(?<state>[是否]+)</td>[\\s\\S.]+?<a.+href=\"(?<url>.+?)\".+>\\s+(?<title>.+)；");
                var o = new Order();
                if ((match.Groups["type"].Value.Contains("故障处理工单") || match.Groups["type"].Value.Contains("事件告警")) && match.Groups["state"].Value.Contains("否"))
                {
                    o.title = match.Groups["title"].Value;
                    o.url = match.Groups["url"].Value;
                    o.type = match.Groups["type"].Value;
                    o.state = match.Groups["state"].Value;
                    o.time = match.Groups["time"].Value;
                    o.id = match.Groups["id"].Value;
                    orderList.Add(o);
                }
                else continue;                             
            }
            return orderList;
        }
    }
}